package com.xueyi.aixihu.controller;

import com.alibaba.fastjson.JSON;
import com.xueyi.aixihu.controller.base.BaseController;
import com.xueyi.aixihu.entity.po.DtAuthRule;
import com.xueyi.aixihu.entity.vo.DtAuthGroupVo;
import com.xueyi.aixihu.entity.vo.DtUsersVo;
import com.xueyi.aixihu.entity.vo.MenuVo;
import com.xueyi.aixihu.exception.SzException;
import com.xueyi.aixihu.mapper.MenuMapper;
import com.xueyi.aixihu.response.base.BaseResponse;
import com.xueyi.aixihu.status.SystemCode;
import com.xueyi.aixihu.utils.FinalUtil;
import com.xueyi.aixihu.utils.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 描述 菜单Controller
 *
 * @auther 俞烨
 * @date 2022-08-13 16
 */
@RestController
public class MenuController extends BaseController {

    @Autowired
    MenuMapper menuMapper;

    /**
     * 查询当前登陆用户的菜单信息
     *
     * @return
     */
    @RequestMapping("/findMenu")
    public BaseResponse<List<MenuVo>> findMenu() throws SzException {

        DtUsersVo currentUser = DtUsersVo.findCurrentUser();

        // 查询当前登陆的用户有什么权限列表
        String currentRuleText = getCurrentRuleText(currentUser);
        // 查询列表对应的id信息以及分组的信息
        List<DtAuthRule> authById = menuMapper.findAuthById(currentRuleText);
        List<MenuVo> dtAuthRules = new ArrayList<>();
        if (!CollectionUtils.isEmpty(authById)) {
            Map<Integer, List<DtAuthRule>> groupByPid = authById.stream().collect(Collectors.groupingBy(DtAuthRule::getPid));
            List<DtAuthRule> parentList = groupByPid.get(0);
            for (DtAuthRule dtAuthRule : parentList) {
                dtAuthRules.add(new MenuVo(dtAuthRule.getId(), groupByPid.get(dtAuthRule.getId())));
            }
        }
        return BaseResponse.success(dtAuthRules);
    }

    private String getCurrentRuleText(DtUsersVo currentUser) throws SzException {
        List<String> roleList = currentUser.getRoleList();
        if (!CollectionUtils.isEmpty(roleList)) {
            Jedis jedis = JedisUtil.getJedis();
            try {
                StringJoiner sj = new StringJoiner(",");
                for (String roleId : roleList) {
                    String roleJson = jedis.hget("ROLE_ALL_CDZ", "ROLE:ID_CDZ" + roleId);
                    if (StringUtils.isEmpty(roleJson)) {
                        continue;
                    }

                    // 转换成对象
                    DtAuthGroupVo dtAuthGroupVo = JSON.parseObject(roleJson, DtAuthGroupVo.class);
                    sj.add(dtAuthGroupVo.getDtAuthGroup().getRules());
                }
                return sj.toString();
            } finally {
                FinalUtil.unLockAndCloseRedis(jedis);
            }
        }

        throw new SzException(SystemCode.USER_JURISDICTION_ERROR, "暂无任何权限");
    }
}
